package com.alks.function.service.impl.biocontrol.deptwork;

import com.alks.common.data.thread.UserIdThread;
import com.alks.entity.data.entity.CpBaseDetail;
import com.alks.entity.data.entity.CpBaseHead;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.mapper.biocontrol.deptwork.BC_DW_CpBaseDetailMapper;
import com.alks.function.mapper.biocontrol.deptwork.BC_DW_CpBaseHeadMapper;
import com.alks.function.service.biocontrol.deptwork.ICpBaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.alks.entity.data.enums.ResultCodeEnum.INPUT_ERROR;

@Service
@Slf4j
@RequiredArgsConstructor
public class CpBaseServiceImpl extends ServiceImpl<BC_DW_CpBaseHeadMapper, CpBaseHead> implements ICpBaseService {
    private final BC_DW_CpBaseHeadMapper headMapper;
    private final BC_DW_CpBaseDetailMapper detailMapper;


    @Override
    public ResponseInfo getCpBaseHeadQuery() {
        /*获取数据*/
        List<CpBaseHead> dtos = lambdaQuery()
                .eq(CpBaseHead::getCompanyId, UserIdThread.get().getCompanyId()).list();
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo cpBaseHeadUpdate(CpBaseHead request, String baseTypeOld) {
        /*判断入参合法性*/
        if (request.getBaseType() == null) {
            return ResponseInfo.error("类型代码不能为空", INPUT_ERROR.getCode());
        }
        Integer count = lambdaQuery()
                .eq(CpBaseHead::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpBaseHead::getBaseType, baseTypeOld).count();
        if (!request.getBaseType().equals(baseTypeOld) && count > 0) {
            return ResponseInfo.error("类型代码不能重复", INPUT_ERROR.getCode());
        }
        /*更新数据*/
        lambdaUpdate()
                .eq(CpBaseHead::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpBaseHead::getBaseType, baseTypeOld)
                .set(CpBaseHead::getBaseType, request.getBaseType())
                .set(CpBaseHead::getBaseName, request.getBaseName())
                .update();
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo cpBaseHeadAdd(CpBaseHead request) {
        /*入参合法性*/
        Integer count = lambdaQuery()
                .eq(CpBaseHead::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpBaseHead::getBaseType, request.getBaseType()).count();
        if (count > 0) {
            return ResponseInfo.error("类型代码不能重复", INPUT_ERROR.getCode());
        }
        /*补全并写入数据*/
        request.setCompanyId(UserIdThread.get().getCompanyId());
        headMapper.insert(request);
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo cpBaseHeadDelete(String baseType) {
        /*删除数据*/
        LambdaQueryWrapper<CpBaseHead> wrapper =new LambdaQueryWrapper<CpBaseHead>()
                .eq(CpBaseHead::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpBaseHead::getBaseType, baseType);
        headMapper.delete(wrapper);
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo getCpBaseDetQuery(String baseType) {
        /*获取数据*/
        LambdaQueryWrapper<CpBaseDetail> wrapper = new LambdaQueryWrapper<CpBaseDetail>()
                .eq(CpBaseDetail::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpBaseDetail::getBaseType, baseType)
                .orderByAsc(CpBaseDetail::getSeqNo);
        List<CpBaseDetail> dtos = detailMapper.selectList(wrapper);
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo cpBaseDetUpdate(CpBaseDetail request, String baseTypeOld, String itemCodeOld) {
        /*判断入参合法性*/
        if (request.getBaseType() == null || request.getBaseType().isEmpty()
                || request.getItemCode() == null || request.getItemCode().isEmpty()) {
            log.error("代码或者归属类型代码为空");
            return ResponseInfo.error("代码不能为空", INPUT_ERROR.getCode());
        }
        if (!request.getItemCode().equals(request.getItemCode())) {
            LambdaQueryWrapper<CpBaseDetail> wrapper = new LambdaQueryWrapper<CpBaseDetail>()
                    .eq(CpBaseDetail::getCompanyId, UserIdThread.get().getCompanyId())
                    .eq(CpBaseDetail::getBaseType, baseTypeOld)
                    .eq(CpBaseDetail::getItemCode, request.getItemCode());
            Integer count = detailMapper.selectCount(wrapper);
            if (count>0){
                return ResponseInfo.error("代码重复", INPUT_ERROR.getCode());
            }
        }
        /*更新数据*/
        LambdaUpdateWrapper<CpBaseDetail> wrapperUp = new LambdaUpdateWrapper<CpBaseDetail>()
                .eq(CpBaseDetail::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpBaseDetail::getBaseType, baseTypeOld)
                .eq(CpBaseDetail::getItemCode, itemCodeOld);
        request.setCompanyId(UserIdThread.get().getCompanyId());
        detailMapper.update(request,wrapperUp);
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo cpBaseDetAdd(CpBaseDetail request) {
        /*判断入参合法性*/
        if (request.getBaseType() == null || request.getBaseType().isEmpty()
                || request.getItemCode() == null || request.getItemCode().isEmpty()) {
            log.error("代码或者归属类型代码为空");
            return ResponseInfo.error("代码不能为空", INPUT_ERROR.getCode());
        }
        if (!request.getItemCode().equals(request.getItemCode())) {
            LambdaQueryWrapper<CpBaseDetail> wrapper = new LambdaQueryWrapper<CpBaseDetail>()
                    .eq(CpBaseDetail::getCompanyId, UserIdThread.get().getCompanyId())
                    .eq(CpBaseDetail::getBaseType, request.getBaseType())
                    .eq(CpBaseDetail::getItemCode, request.getItemCode());
            Integer count = detailMapper.selectCount(wrapper);
            if (count>0){
                return ResponseInfo.error("代码重复", INPUT_ERROR.getCode());
            }
        }
        /*写入数据*/
        request.setCompanyId(UserIdThread.get().getCompanyId());
        detailMapper.insert(request);
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo cpBaseDetDelete(String baseType, String itemCode) {
        /*删除数据*/
        LambdaQueryWrapper<CpBaseDetail> wrapper = new LambdaQueryWrapper<CpBaseDetail>()
                .eq(CpBaseDetail::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(CpBaseDetail::getBaseType, baseType)
                .eq(CpBaseDetail::getItemCode, itemCode);
        detailMapper.delete(wrapper);
        return ResponseInfo.ok();
    }
}
